package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.service.SysRoleService;
import com.b511.swpu.cementbackend.dto.input.SysRoleInputDto;
import com.b511.swpu.cementbackend.dto.output.SysMenuOutputDto;
import com.b511.swpu.cementbackend.entity.SysMenu;
import com.b511.swpu.cementbackend.entity.SysRole;
import com.b511.swpu.cementbackend.repository.SysMenuRepository;
import com.b511.swpu.cementbackend.repository.SysRoleRepository;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "角色管理", tags = {"角色管理"})
@RestController
@RequestMapping("/role")
public class SysRoleController {

    private final SysRoleRepository sysRoleRepository;

    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleRepository sysRoleRepository, SysRoleService sysRoleService) {
        this.sysRoleRepository = sysRoleRepository;
        this.sysRoleService = sysRoleService;
    }

    @GetMapping
    public R getAllRole() {
        return sysRoleService.getAllSysRole();
    }

    @PostMapping
    @Transactional
    public R addRole(@RequestBody @Validated SysRoleInputDto sysRoleInputDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleInputDto, sysRole);
        sysRoleRepository.save(sysRole);
        return R.ok();
    }

    @PutMapping
    @Transactional
    public R updateRole(@RequestBody SysRole sysRole) {
        sysRoleRepository.save(sysRole);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Transactional
    public R deleteRole(@PathVariable String id) {
        sysRoleRepository.deleteById(id);
        return R.ok();
    }

    @GetMapping("/menu")
    public R getAllMenu() {
        return sysRoleService.getAllSysMenu();
    }
}
